package com.yungu.swift.assets.passenger.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.yungu.swift.assets.passenger.dao.PassengerAddressMapper;
import com.yungu.swift.assets.passenger.model.dto.PassengerAddressDto;
import com.yungu.swift.assets.passenger.model.param.ApiPassengerAddressParam;
import com.yungu.swift.assets.passenger.service.PassengerAddressService;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.base.mapper.IMapper;
import com.yungu.swift.base.service.impl.BaseServiceImpl;
import com.yungu.swift.utils.MapUtils;
import com.yungu.swift.utils.StringUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 乘客地址业务逻辑处理接口实现类
 *
 * @author cuixiuyin - Generate
 * @version 1.1.1-SNAPSHOT
 * @since 2018-07-04 13:50
 */
@Service
public class PassengerAddressServiceImpl extends BaseServiceImpl<PassengerAddressDto> implements PassengerAddressService {
    @Autowired
    private PassengerAddressMapper passengerAddressMapper;

    @Override
    protected IMapper<PassengerAddressDto> getMapper() {
        return passengerAddressMapper;
    }

    @Override
    public ResponseData<List<PassengerAddressDto>> addressList(String uuid) {
        //通过乘客uuid获取地址列表
        Map<String, Object> map = MapUtils.build();
        map.put("passengerUuid", uuid);
        return ResponseData.buildSuccessResponse(passengerAddressMapper.list(map));
    }

    @Override
    public ResponseData<PassengerAddressDto> addAddress(ApiPassengerAddressParam addressParam) {
        PassengerAddressDto passengerAddressDto = new PassengerAddressDto();
        BeanUtils.copyProperties(addressParam, passengerAddressDto);
        passengerAddressDto.setPassengerUuid(addressParam.getUserUuid());
        // 设置或者修改地址改为同一个接口
        Map<String, Object> params = MapUtils.build(4);
        params.put("passengerUuid", addressParam.getUserUuid());
        params.put("type", addressParam.getType());
        List<PassengerAddressDto> passengerAddressList = passengerAddressMapper.list(params);
        if (CollectionUtils.isEmpty(passengerAddressList)) {
            passengerAddressDto.setUuid(StringUtils.buildUUID());
            passengerAddressDto.setCreateOn(new Date());
            passengerAddressDto.setCreateBy(addressParam.getUserUuid());
            passengerAddressMapper.add(passengerAddressDto);
            return ResponseData.buildSuccessResponse("添加成功", passengerAddressDto);
        }
        passengerAddressDto.setUuid(passengerAddressList.get(0).getUuid());
        passengerAddressDto.setUpdateOn(new Date());
        passengerAddressDto.setUpdateBy(addressParam.getUserUuid());
        passengerAddressMapper.edit(passengerAddressDto);
        return ResponseData.buildSuccessResponse("修改成功", passengerAddressDto);
    }

    @Override
    public ResponseData<PassengerAddressDto> getAddressInfo(String passengerUuid, Integer type) {
        Map<String, Object> params = MapUtils.build();
        params.put("passengerUuid", passengerUuid);
        params.put("type", type);
        List<PassengerAddressDto> list = passengerAddressMapper.list(params);
        if (CollectionUtils.isNotEmpty(list)) {
            return ResponseData.buildSuccessResponse(list.get(0));
        }
        return ResponseData.buildSuccessResponse(null);
    }


}